package com.stationbuilding.activity.message;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.stationbuilding.R;
import com.stationbuilding.adapter.FragmentPagerAdapter;
import com.stationbuilding.fragment.message.NoticeFragment;
import com.stationbuilding.fragment.message.TaskFragment;
import com.stationbuilding.utils.TabLayoutUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MessageActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> titleList = Arrays.asList("任务消息", "通知提醒");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        // 初始化toolbar
        initToolbar();
        initView();
    }

    private void initToolbar() {
        // 点击事件监听监听
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.back:
                        finish();
                        break;
                }
            }
        };
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        ImageView back = (ImageView) toolbar.findViewById(R.id.back);
        TextView title = (TextView) toolbar.findViewById(R.id.title);
        title.setText(getResources().getText(R.string.message_center));
        back.setOnClickListener(onClickListener);
    }

    private void initView() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new TaskFragment());
        fragmentList.add(new NoticeFragment());
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        // 将ViewPager关联到TabLayout上
        tabLayout.setupWithViewPager(viewPager);

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                TabLayoutUtil.setIndicator(tabLayout, 55, 55);
            }
        });
    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
        TextView tv = (TextView) view.findViewById(R.id.text);
        tv.setText(titleList.get(position));
        return view;
    }
}
